package com.example.supplychainbackend.controller;

import com.example.supplychainbackend.controller.dto.DataResponse;
import com.example.supplychainbackend.controller.dto.blockchain.UploadInfo;
import com.example.supplychainbackend.controller.dto.enterprise.EnterpriseCertDto;
import com.example.supplychainbackend.controller.dto.enterprise.EnterpriseDto;
import com.example.supplychainbackend.controller.dto.sales.*;
import com.example.supplychainbackend.controller.dto.user.UserSigDto;
import com.example.supplychainbackend.service.DesignService;
import com.example.supplychainbackend.service.SalesService;
import com.example.supplychainbackend.tool.JwtUtil;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/sales")
@AllArgsConstructor
public class SalesController {
    private final SalesService salesService;
    private final DesignService designService;

    @PostMapping("/customer")
    public DataResponse<String> createCustomer(@RequestBody CustomerDetailDto form) {
        String infoId = salesService.createCustomer(form);
        return DataResponse.success(infoId);
    }

    @PutMapping("/customer/{id}")
    public DataResponse<CustomerDetailDto> updateCustomer(@PathVariable String id, @RequestBody CustomerDetailDto form) {
        CustomerDetailDto res = salesService.updateCustomer(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/customer/list")
    public DataResponse<List<CustomerDto>> listCustomers() {
        List<CustomerDto> customers = salesService.getAllCustomer(0L);
        return DataResponse.success(customers);
    }

    @GetMapping("/customer/{id}")
    public DataResponse<CustomerDetailDto> getCustomer(@PathVariable String id) {
        CustomerDetailDto res = salesService.getCustomer(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/customer/{id}")
    public DataResponse<String> deleteCustomer(@PathVariable String id) {
        salesService.deleteCustomer(id);
        return DataResponse.success(id);
    }

    @PostMapping("/contractDetail/{id}/chain")
    public DataResponse<UploadInfo> uploadToChainContract(@PathVariable String id) {
        UploadInfo txInfo = salesService.uploadToChainContract(id);
        return DataResponse.success(txInfo);
    }

    @PostMapping("/contractDetail")
    public DataResponse<String> createContract(@RequestHeader("Authorization") String authHeader,
                                               @RequestBody ContractDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        String contractNumber = salesService.createContract(form);
        return DataResponse.success(contractNumber);
    }

    @PutMapping("/contractDetail/{id}")
    public DataResponse<ContractDto> updateContract(@RequestHeader("Authorization") String authHeader,
                                                    @PathVariable String id, @RequestBody ContractDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        ContractDto res = salesService.updateContract(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/contract/list")
    public DataResponse<List<ContractDto>> listContracts(@RequestHeader("Authorization") String authHeader) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        List<ContractDto> contracts = salesService.getAllContract(Long.valueOf(eid));
        return DataResponse.success(contracts);
    }

    @GetMapping("/contractDetail/{id}")
    public DataResponse<ContractDto> getContract(@PathVariable String id) {
        ContractDto res = salesService.getContract(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/contract/{id}")
    public DataResponse<String> deleteContract(@PathVariable String id) {
        salesService.deleteContract(id);
        return DataResponse.success(id);
    }

    //订单增删改查
    @PostMapping("/order/{id}/chain")
    public DataResponse<UploadInfo> uploadToChainOrder(@PathVariable String id) {
        UploadInfo txInfo = salesService.uploadToChainOrder(id);
        // trigger boms upload
        designService.uploadToChainBOMsByOrder(id);
        return DataResponse.success(txInfo);
    }

    @PostMapping("/order")
    public DataResponse<String> createOrder(@RequestHeader("Authorization") String authHeader,
                                            @RequestBody @Valid OrderDetailDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        String infoId = salesService.createOrder(form);
        return DataResponse.success(infoId);
    }

    @PutMapping("/order/{id}")
    public DataResponse<OrderDetailDto> updateOrder(@RequestHeader("Authorization") String authHeader,
                                                    @PathVariable String id, @RequestBody OrderDetailDto form) {
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        form.setUserName(userSigDto.getUserName());
        form.setUserId(userSigDto.getUserId());
        form.setEnterpriseName(userSigDto.getEnterpriseName());
        form.setEnterpriseId(userSigDto.getEnterpriseId());
        OrderDetailDto res = salesService.updateOrder(id, form);
        return DataResponse.success(res);
    }

    @GetMapping("/order/list")
    public DataResponse<List<OrderListItemDto>> listOrders(@RequestHeader("Authorization") String authHeader) {
        List<OrderListItemDto> orders = salesService.listOrderItems();
        UserSigDto userSigDto = JwtUtil.getUserSignatureFromToken(authHeader);
        String eid = userSigDto.getEnterpriseId();
        if (eid != null) {
            orders = salesService.listOrderItemsByEid(eid);
        }
        return DataResponse.success(orders);
    }

    @GetMapping("/order/{id}")
    public DataResponse<OrderDetailDto> getOrder(@PathVariable String id) {
        OrderDetailDto res = salesService.getOrder(id);
        return DataResponse.success(res);
    }

    @DeleteMapping("/order/{id}")
    public DataResponse<String> deleteOrder(@PathVariable String id) {
        salesService.deleteOrder(id);
        return DataResponse.success(id);
    }

    @ExceptionHandler(Exception.class)
    public DataResponse<Exception> salesErrorHandler(Exception e) {
        return DataResponse.error(500,e);
    }

}
